<?php
/**
 * Contents Controller
 */
namespace PUF;

class Contents extends FAController {
	
	protected $models = 'Content';
	
	/*protected function _before() {
		parent::_before();
		$this->View->setTitle('Fergie Argentina');
	}*/
	
	public function home() {
		$this->View->setLayout('home');
		$this->View->setView('Contents/listing');
		
		$this->listing();
	}
	
	public function listing() {
		$page = 1;
		if ($this->paramExists('page')) {
			$page = $this->getParam('page');
		}
		
		$conditions = [
			'status = ' . CONTENT_STATUS_ACTIVE,
			'category_id != ' . CATEGORY_PAGES
		];
		
		$totalContents = Content::count([
			'conditions' => $conditions
		]);
		$this->setVar('totalContents', $totalContents);
		$this->setVar('totalPages', ceil($totalContents / PAGINATE_LIMIT_HOME));
		$this->setVar('page', $page);
		
		$contents = Content::find([
			'conditions'	=> $conditions,
			'limit'			=> PAGINATE_LIMIT_HOME,
			'offset'		=> ($page - 1) * PAGINATE_LIMIT_HOME,
			'order'			=> 'date DESC'
		]);
		$this->setVar('contents', $contents);
	}
	
	public function detail($id) {
		$content = Content::findBy('id', $id);
		$this->setVar('content', $content);
	}
}